package com.epoint.webapp.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.epoint.webapp.dao.PayItemDAO;
import com.epoint.webapp.dao.PayMoneyDAO;
import com.epoint.webapp.dao.PayrollDAO;
import com.epoint.webapp.dao.VentureChecklistDAO;
import com.epoint.webapp.entity.HumanResourceContent;
import com.epoint.webapp.entity.Member;
import com.epoint.webapp.entity.PayItem;
import com.epoint.webapp.entity.PayMoney;
import com.epoint.webapp.entity.Payroll;
import com.epoint.webapp.form.ExpenditureForm;
import com.epoint.webapp.util.DateConversion;

@Controller
public class ExpenditureStructureController {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-module.xml");

	@RequestMapping(value = "/founded", method = RequestMethod.GET)
	public ModelAndView founded(HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();
		PayMoneyDAO payMoneyDAO = (PayMoneyDAO) context.getBean("payMoneyDAO");
		Member loginMember = (Member) session.getAttribute("loginMember");
		if(loginMember != null){
			String account = loginMember.getAccount();
            List<PayMoney> fixedPayMoney = new ArrayList<PayMoney>();
            fixedPayMoney.add(payMoneyDAO.getSinglePayMoneyByItemID(account, 2111));
            fixedPayMoney.add(payMoneyDAO.getSinglePayMoneyByItemID(account, 2112));
            List<PayMoney> cardPayMoney = payMoneyDAO.getPayMoneyByItemID(account,2113);
            List<PayMoney> lawyerPayMoney = payMoneyDAO.getPayMoneyByItemID(account, 2114);
            List<PayMoney> accountPayMoney = payMoneyDAO.getPayMoneyByItemID(account, 2115);
            List<PayMoney> dynamicPayMoney = new ArrayList<PayMoney>();
            int[] dynamicTypeCount = { cardPayMoney.size(), lawyerPayMoney.size(),accountPayMoney.size() };
            dynamicPayMoney.addAll(cardPayMoney);
            dynamicPayMoney.addAll(lawyerPayMoney);
            dynamicPayMoney.addAll(accountPayMoney);
            model.addObject("getMember", loginMember);
            model.addObject("fixedPayMoney", fixedPayMoney);
            model.addObject("dynamicPayMoney", dynamicPayMoney);
            model.addObject("dynamicTypeCount", dynamicTypeCount);
            model.setViewName("founded");
		}else{
			model.setViewName("memberLogin");
		}
            return model;
	}
       
    @RequestMapping(value = "/placeComplete", method = RequestMethod.GET)
    public ModelAndView placeComplete(HttpSession session) throws IOException {
    	ModelAndView model = new ModelAndView();
    	PayMoneyDAO payMoneyDAO = (PayMoneyDAO) context.getBean("payMoneyDAO");
    	Member loginMember = (Member) session.getAttribute("loginMember");
        if(loginMember != null){
        	String account = loginMember.getAccount();
        	List<PayMoney> fixedPayMoney = new ArrayList<PayMoney>();
        	fixedPayMoney.add(payMoneyDAO.getSinglePayMoneyByItemID(account, 2121));
        	fixedPayMoney.add(payMoneyDAO.getSinglePayMoneyByItemID(account, 2122));
            fixedPayMoney.add(payMoneyDAO.getSinglePayMoneyByItemID(account, 2123));
            fixedPayMoney.add(payMoneyDAO.getSinglePayMoneyByItemID(account, 2124));
            List<PayMoney> decoratedPayMoney = payMoneyDAO.getPayMoneyByItemID(account,2125);
            List<PayMoney> equipmentPayMoney = payMoneyDAO.getPayMoneyByItemID(account, 2126);
            List<PayMoney> deskPayMoney = payMoneyDAO.getPayMoneyByItemID(account, 2127);
            List<PayMoney> dynamicPayMoney = new ArrayList<PayMoney>();
            int[] dynamicTypeCount = { decoratedPayMoney.size(), equipmentPayMoney.size(),deskPayMoney.size() };
            dynamicPayMoney.addAll(decoratedPayMoney);
            dynamicPayMoney.addAll(equipmentPayMoney);
            dynamicPayMoney.addAll(deskPayMoney);
            model.addObject("getMember", loginMember);
            model.addObject("fixedPayMoney", fixedPayMoney);
            model.addObject("dynamicPayMoney", dynamicPayMoney);
            model.addObject("dynamicTypeCount", dynamicTypeCount);
            model.setViewName("placeComplete");
        }else{
        	model.setViewName("memberLogin");
        }
        return model;
	}
       
	@RequestMapping(value = "/personPlace", method = RequestMethod.GET)
	public ModelAndView personPlace(HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();
        Member loginMember = (Member) session.getAttribute("loginMember");
        if(loginMember != null){
        	
        	/* To get the number of the staff */
    		VentureChecklistDAO ventureChecklistDAO = (VentureChecklistDAO)context.getBean("ventureChecklistDAO");
    		HumanResourceContent getHumanResourceContentByMember = ventureChecklistDAO.getHumanResourceContentByMember(loginMember);
    		
    		PayrollDAO payrollDAO = (PayrollDAO)context.getBean("payrollDAO");
    		List<Payroll> payrollList = new ArrayList<Payroll>();
    		payrollList = payrollDAO.getList(loginMember.getAccount());
    		
    		PayMoneyDAO payMoneyDAO = (PayMoneyDAO)context.getBean("payMoneyDAO");
    		String account = loginMember.getAccount();
            List<PayMoney> fixedPayMoney = new ArrayList<PayMoney>();
            fixedPayMoney.add(payMoneyDAO.getSinglePayMoneyByItemID(account, 2131));
            fixedPayMoney.add(payMoneyDAO.getSinglePayMoneyByItemID(account, 2132));
            List<PayMoney> officialPayMoney = payMoneyDAO.getPayMoneyByItemID(account,2133);
            List<PayMoney> trainingPayMoney = payMoneyDAO.getPayMoneyByItemID(account, 2134);
            
            List<PayMoney> dynamicPayMoney = new ArrayList<PayMoney>();
            int[] dynamicTypeCount = { officialPayMoney.size(), trainingPayMoney.size()};
            dynamicPayMoney.addAll(officialPayMoney);
            dynamicPayMoney.addAll(trainingPayMoney);
            
            
            model.addObject("fixedPayMoney", fixedPayMoney);
            model.addObject("dynamicPayMoney", dynamicPayMoney);
            model.addObject("dynamicTypeCount", dynamicTypeCount);
            
    		
    		// for personPlace page forEach loop to generate table row
    		
    		int m0 = getHumanResourceContentByMember.getM0();
    		int m1 = getHumanResourceContentByMember.getM1();
    		int m2 = getHumanResourceContentByMember.getM2();
    		int m3 = getHumanResourceContentByMember.getM3();
    		int m4 = getHumanResourceContentByMember.getM4();
    		int m5 = getHumanResourceContentByMember.getM5();
    		int m6 = getHumanResourceContentByMember.getM6();
    		int m7 = getHumanResourceContentByMember.getM7();
    		int m8 = getHumanResourceContentByMember.getM8();
    		int m9 = getHumanResourceContentByMember.getM9();
    		int m10 = getHumanResourceContentByMember.getM10();
    		int m11 = getHumanResourceContentByMember.getM11();
    		int m12 = getHumanResourceContentByMember.getM12();
    		
    		
    		int r0 = getHumanResourceContentByMember.getR0();
    		int r1 = getHumanResourceContentByMember.getR1();
    		int r2 = getHumanResourceContentByMember.getR2();
    		int r3 = getHumanResourceContentByMember.getR3();
    		int r4 = getHumanResourceContentByMember.getR4();
    		int r5 = getHumanResourceContentByMember.getR5();
    		int r6 = getHumanResourceContentByMember.getR6();
    		int r7 = getHumanResourceContentByMember.getR7();
    		int r8 = getHumanResourceContentByMember.getR8();
    		int r9 = getHumanResourceContentByMember.getR9();
    		int r10 = getHumanResourceContentByMember.getR10();
    		int r11 = getHumanResourceContentByMember.getR11();
    		int r12 = getHumanResourceContentByMember.getR12();
    		
    		int s0 = getHumanResourceContentByMember.getS0();
    		int s1 = getHumanResourceContentByMember.getS1();
    		int s2 = getHumanResourceContentByMember.getS2();
    		int s3 = getHumanResourceContentByMember.getS3();
    		int s4 = getHumanResourceContentByMember.getS4();
    		int s5 = getHumanResourceContentByMember.getS5();
    		int s6 = getHumanResourceContentByMember.getS6();
    		int s7 = getHumanResourceContentByMember.getS7();
    		int s8 = getHumanResourceContentByMember.getS8();
    		int s9 = getHumanResourceContentByMember.getS9();
    		int s10 = getHumanResourceContentByMember.getS10();
    		int s11 = getHumanResourceContentByMember.getS11();
    		int s12 = getHumanResourceContentByMember.getS12();
    		
    		int o0 = getHumanResourceContentByMember.getO0();
    		int o1 = getHumanResourceContentByMember.getO1();
    		int o2 = getHumanResourceContentByMember.getO2();
    		int o3 = getHumanResourceContentByMember.getO3();
    		int o4 = getHumanResourceContentByMember.getO4();
    		int o5 = getHumanResourceContentByMember.getO5();
    		int o6 = getHumanResourceContentByMember.getO6();
    		int o7 = getHumanResourceContentByMember.getO7();
    		int o8 = getHumanResourceContentByMember.getO8();
    		int o9 = getHumanResourceContentByMember.getO9();
    		int o10 = getHumanResourceContentByMember.getO10();
    		int o11 = getHumanResourceContentByMember.getO11();
    		int o12 = getHumanResourceContentByMember.getO12();
    		
    		//m0
    		model.addObject("getM0Start",0);
    		model.addObject("getM0End",  m0-1);
    		//r0
    		model.addObject("getR0Start",m0);
    		model.addObject("getR0End",  m0+r0-1);
    		//s0
    		model.addObject("getS0Start",m0+r0);
    		model.addObject("getS0End",  m0+r0+s0-1);
    		//o0
    		model.addObject("getO0Start",m0+r0+s0);
    		model.addObject("getO0End",  m0+r0+s0+o0-1);
    		
    		//m1
    		model.addObject("getM1Start",m0+r0+s0+o0);
    		model.addObject("getM1End",  m0+r0+s0+o0+m1-1);
    		//r1
    		model.addObject("getR1Start",m0+r0+s0+o0+m1);
    		model.addObject("getR1End",  m0+r0+s0+o0+m1+r1-1);
    		//s1
    		model.addObject("getS1Start",m0+r0+s0+o0+m1+r1);
    		model.addObject("getS1End",  m0+r0+s0+o0+m1+r1+s1-1);
    		//o1
    		model.addObject("getO1Start",m0+r0+s0+o0+m1+r1+s1);
    		model.addObject("getO1End",  m0+r0+s0+o0+m1+r1+s1+o1-1);
    		
    		//m2
    		model.addObject("getM2Start",m0+r0+s0+o0+m1+r1+s1+o1);
    		model.addObject("getM2End",  m0+r0+s0+o0+m1+r1+s1+o1+m2-1);
    		//r2
    		model.addObject("getR2Start",m0+r0+s0+o0+m1+r1+s1+o1+m2);
    		model.addObject("getR2End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2-1);
    		//s2
    		model.addObject("getS2Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2);
    		model.addObject("getS2End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2-1);
    		//o2
    		model.addObject("getO2Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2);
    		model.addObject("getO2End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2-1);
    		
    		//m3
    		model.addObject("getM3Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2);
    		model.addObject("getM3End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3-1);    		
    		//r3
    		model.addObject("getR3Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3);
    		model.addObject("getR3End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3-1);
    		//s3
    		model.addObject("getS3Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3);
    		model.addObject("getS3End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3-1);
    		//o3
    		model.addObject("getO3Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3);
    		model.addObject("getO3End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3-1);
    		
    		//m4
    		model.addObject("getM4Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3);
    		model.addObject("getM4End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4-1);
    		//r4
    		model.addObject("getR4Start",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4);
    		model.addObject("getR4End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4-1);
    		//s4
    		model.addObject("getS4Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4);
    		model.addObject("getS4End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4-1);
    		//o4
    		model.addObject("getO4Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4);
    		model.addObject("getO4End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4-1);
    		
    		//m5
    		model.addObject("getM5Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4);
    		model.addObject("getM5End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5-1);
    		//r5
    		model.addObject("getR5Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5);
    		model.addObject("getR5End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5-1);
    		//s5
    		model.addObject("getS5Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5);
    		model.addObject("getS5End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5-1);
    		//o5
    		model.addObject("getO5Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5);
    		model.addObject("getO5End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5-1);
    		
    		//m6
    		model.addObject("getM6Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5);
    		model.addObject("getM6End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6-1);
    		//r6
    		model.addObject("getR6Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6);
    		model.addObject("getR6End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6-1);
    		//s6
    		model.addObject("getS6Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6);
    		model.addObject("getS6End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6-1);
    		//o6
    		model.addObject("getO6Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6);
    		model.addObject("getO6End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6-1);
    		
    		//m7
    		model.addObject("getM7Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6);
    		model.addObject("getM7End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7-1);
    		//r7
    		model.addObject("getR7Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7);
    		model.addObject("getR7End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7-1);
    		//s7
    		model.addObject("getS7Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7);
    		model.addObject("getS7End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7-1);
    		//o7
    		model.addObject("getO7Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7);
    		model.addObject("getO7End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7-1);
    		
    		//m8
    		model.addObject("getM8Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7);
    		model.addObject("getM8End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8-1);
    		//r8
    		model.addObject("getR8Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8);
    		model.addObject("getR8End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8-1);
    		//s8
    		model.addObject("getS8Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8);
    		model.addObject("getS8End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8-1);
    		//o8
    		model.addObject("getO8Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8);
    		model.addObject("getO8End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8-1);
    		
    		//m9
    		model.addObject("getM9Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8);
    		model.addObject("getM9End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9-1);
    		//r9
    		model.addObject("getR9Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9);
    		model.addObject("getR9End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9-1);
    		//s9
    		model.addObject("getS9Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9);
    		model.addObject("getS9End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9-1);
    		//o9
    		model.addObject("getO9Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9);
    		model.addObject("getO9End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9-1);

    		//m10
    		model.addObject("getM10Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9);
    		model.addObject("getM10End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10-1);
    		//r10
    		model.addObject("getR10Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10);
    		model.addObject("getR10End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10-1);
    		//s10
    		model.addObject("getS10Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10);
    		model.addObject("getS10End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10-1);
    		//o10
    		model.addObject("getO10Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10);
    		model.addObject("getO10End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10-1);
    		
    		//m11
    		model.addObject("getM11Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10);
    		model.addObject("getM11End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11-1);
    		//r11
    		model.addObject("getR11Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11);
    		model.addObject("getR11End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11-1);
    		//s11
    		model.addObject("getS11Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11);
    		model.addObject("getS11End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11+s11-1);
    		//o11
    		model.addObject("getO11Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11+s11);
    		model.addObject("getO11End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11+s11+o11-1);
    		
    		//m12
    		model.addObject("getM12Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11+s11+o11);
    		model.addObject("getM12End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11+s11+o11+m12-1);
    		//r12
    		model.addObject("getR12Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11+s11+o11+m12);
    		model.addObject("getR12End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11+s11+o11+m12+r12-1);
    		//s12
    		model.addObject("getS12Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11+s11+o11+m12+r12);
    		model.addObject("getS12End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11+s11+o11+m12+r12+s12-1);
    		//o12
    		model.addObject("getO12Start",m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11+s11+o11+m12+r12+s12);
    		model.addObject("getO12End",  m0+r0+s0+o0+m1+r1+s1+o1+m2+r2+s2+o2+m3+r3+s3+o3+m4+r4+s4+o4+m5+r5+s5+o5+m6+r6+s6+o6+m7+r7+s7+o7+m8+r8+s8+o8+m9+r9+s9+o9+m10+r10+s10+o10+m11+r11+s11+o11+m12+r12+s12+o12-1);
    		
    		model.addObject("payrollList", payrollList);
            model.addObject("getMember", loginMember);            
            model.addObject("getHumanResourceContent", getHumanResourceContentByMember);
            model.setViewName("personPlace");
            
            
            
        }else{
        	model.setViewName("memberLogin");
        }
        return model;
	}
       
	@RequestMapping(value = "/equipmentBuild", method = RequestMethod.GET)
	public ModelAndView equipmentBuild(HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();
		PayMoneyDAO payMoneyDAO = (PayMoneyDAO) context.getBean("payMoneyDAO");
        Member loginMember = (Member) session.getAttribute("loginMember");
        if(loginMember != null){
        	String account = loginMember.getAccount();
            List<PayMoney> cleaningPayMoney = payMoneyDAO.getPayMoneyByItemID(account,2141);
            List<PayMoney> buyingPayMoney = payMoneyDAO.getPayMoneyByItemID(account, 2142);
            List<PayMoney> dynamicPayMoney = new ArrayList<PayMoney>();
            int[] dynamicTypeCount = { cleaningPayMoney.size(), buyingPayMoney.size()};
            dynamicPayMoney.addAll(cleaningPayMoney);
            dynamicPayMoney.addAll(buyingPayMoney);
            model.addObject("getMember", loginMember);
            model.addObject("dynamicPayMoney", dynamicPayMoney);
            model.addObject("dynamicTypeCount", dynamicTypeCount);
            model.setViewName("equipmentBuild");
        }else{
        	model.setViewName("memberLogin");
        }
        return model;
	}
       
	@RequestMapping(value = "/trialProduction", method = RequestMethod.GET)
    public ModelAndView trialProduction(HttpSession session) throws IOException {
    	ModelAndView model = new ModelAndView();
        PayMoneyDAO payMoneyDAO = (PayMoneyDAO) context.getBean("payMoneyDAO");
        Member loginMember = (Member) session.getAttribute("loginMember");
        if(loginMember != null){
        	String account = loginMember.getAccount();
        	List<PayMoney> materialPayMoney = payMoneyDAO.getPayMoneyByItemID(account,2151);
            List<PayMoney> consultingPayMoney = payMoneyDAO.getPayMoneyByItemID(account, 2152);
            List<PayMoney> dynamicPayMoney = new ArrayList<PayMoney>();
            int[] dynamicTypeCount = { materialPayMoney.size(), consultingPayMoney.size()};
            dynamicPayMoney.addAll(materialPayMoney);
            dynamicPayMoney.addAll(consultingPayMoney);
            model.addObject("getMember", loginMember);
            model.addObject("dynamicPayMoney", dynamicPayMoney);
            model.addObject("dynamicTypeCount", dynamicTypeCount);
            model.setViewName("trialProduction");
        }else{
        	model.setViewName("memberLogin");
        }
        return model;
	}
       
	@RequestMapping(value = "/procureMaterial", method = RequestMethod.GET)
	public ModelAndView procureMaterial(HttpSession session) throws IOException {
    	ModelAndView model = new ModelAndView();
        PayMoneyDAO payMoneyDAO = (PayMoneyDAO) context.getBean("payMoneyDAO");
        Member loginMember = (Member) session.getAttribute("loginMember");
        if(loginMember != null){
        	String account = loginMember.getAccount();
            List<PayMoney> materialPayMoney = payMoneyDAO.getPayMoneyByItemID(account,2161);
            List<PayMoney> decoratingPayMoney = payMoneyDAO.getPayMoneyByItemID(account, 2162);
            List<PayMoney> dynamicPayMoney = new ArrayList<PayMoney>();
            int[] dynamicTypeCount = { materialPayMoney.size(), decoratingPayMoney.size()};
            dynamicPayMoney.addAll(materialPayMoney);
            dynamicPayMoney.addAll(decoratingPayMoney);
            model.addObject("getMember", loginMember);
            model.addObject("dynamicPayMoney", dynamicPayMoney);
            model.addObject("dynamicTypeCount", dynamicTypeCount);
            model.setViewName("procureMaterial");
        }else{
        	model.setViewName("memberLogin");
        }
        return model;
	}
       
	@RequestMapping(value = "/trialOperation", method = RequestMethod.GET)
    public ModelAndView trialOperation(HttpSession session) throws IOException {
    	ModelAndView model = new ModelAndView();
        PayMoneyDAO payMoneyDAO = (PayMoneyDAO) context.getBean("payMoneyDAO");
        Member loginMember = (Member) session.getAttribute("loginMember");
        if(loginMember != null){
        	String account = loginMember.getAccount();
        	List<PayMoney> activityPayMoney = payMoneyDAO.getPayMoneyByItemID(account,2171);
            List<PayMoney> part_timePayMoney = payMoneyDAO.getPayMoneyByItemID(account, 2172);
            List<PayMoney> dynamicPayMoney = new ArrayList<PayMoney>();
            int[] dynamicTypeCount = { activityPayMoney.size(), part_timePayMoney.size()};
            dynamicPayMoney.addAll(activityPayMoney);
            dynamicPayMoney.addAll(part_timePayMoney);
            model.addObject("getMember", loginMember);
            model.addObject("dynamicPayMoney", dynamicPayMoney);
            model.addObject("dynamicTypeCount", dynamicTypeCount);
            model.setViewName("trialOperation");
        }else{
        	model.setViewName("memberLogin");
        }
        return model;
	}
       
	@RequestMapping(value = "/developmentMarket", method = RequestMethod.GET)
    public ModelAndView developmentMarket(HttpSession session) throws IOException {
    	ModelAndView model = new ModelAndView();
        PayMoneyDAO payMoneyDAO = (PayMoneyDAO) context.getBean("payMoneyDAO");
        Member loginMember = (Member) session.getAttribute("loginMember");
        if(loginMember != null){
        	String account = loginMember.getAccount();
        	List<PayMoney> travelPayMoney = payMoneyDAO.getPayMoneyByItemID(account,2181);
            List<PayMoney> socialPayMoney = payMoneyDAO.getPayMoneyByItemID(account, 2182);
            List<PayMoney> dynamicPayMoney = new ArrayList<PayMoney>();
            int[] dynamicTypeCount = { travelPayMoney.size(), socialPayMoney.size()};
            dynamicPayMoney.addAll(travelPayMoney);
            dynamicPayMoney.addAll(socialPayMoney);
            model.addObject("getMember", loginMember);
            model.addObject("dynamicPayMoney", dynamicPayMoney);
            model.addObject("dynamicTypeCount", dynamicTypeCount);
            model.setViewName("developmentMarket");
        }else{
        	model.setViewName("memberLogin");
        }
        return model;
	}
       
    @RequestMapping(value = "/marketing", method = RequestMethod.GET)
    public ModelAndView marketing(HttpSession session) throws IOException {
    	ModelAndView model = new ModelAndView();
        PayMoneyDAO payMoneyDAO = (PayMoneyDAO) context.getBean("payMoneyDAO");
        Member loginMember = (Member) session.getAttribute("loginMember");
        if(loginMember != null){
        	String account = loginMember.getAccount();
        	List<PayMoney> paperAdPayMoney = payMoneyDAO.getPayMoneyByItemID(account,2191);
            List<PayMoney> electricalAdPayMoney = payMoneyDAO.getPayMoneyByItemID(account, 2192);
            List<PayMoney> physicalAdPayMoney = payMoneyDAO.getPayMoneyByItemID(account, 2193);
            List<PayMoney> dynamicPayMoney = new ArrayList<PayMoney>();
            int[] dynamicTypeCount = { paperAdPayMoney.size(), electricalAdPayMoney.size(), physicalAdPayMoney.size()};
            dynamicPayMoney.addAll(paperAdPayMoney);
            dynamicPayMoney.addAll(electricalAdPayMoney);
            dynamicPayMoney.addAll(physicalAdPayMoney);
            model.addObject("getMember", loginMember);
            model.addObject("dynamicPayMoney", dynamicPayMoney);
            model.addObject("dynamicTypeCount", dynamicTypeCount);
            model.setViewName("marketing");
        }else{
        	model.setViewName("memberLogin");
        }
        return model;
	}

	@RequestMapping(value = "/addExpenditure", method = RequestMethod.POST)
    public ModelAndView addExpenditure(@ModelAttribute ExpenditureForm expenditureForm,
                        HttpSession session, HttpServletRequest request) throws IOException {
		ModelAndView model = new ModelAndView();
        PayMoneyDAO payMoneyDAO = (PayMoneyDAO) context.getBean("payMoneyDAO");
        PayItemDAO payItemDAO = (PayItemDAO) context.getBean("payItemDAO");
        Member loginMember = (Member) session.getAttribute("loginMember");
        PayMoney payMoney = new PayMoney();
        if(loginMember != null){
        	String account = loginMember.getAccount();
        	payMoney.setAccount(account);

        	int subClassID = Integer.parseInt(expenditureForm.getSubClass());
            List<PayItem> allPayItem = payItemDAO.getAllPayItemBySubClassID(subClassID);
            List<PayMoney> oldFixedPayMoney = new ArrayList<PayMoney>();
            List<PayMoney> oldDynamicPayMoney = new ArrayList<PayMoney>();
            for(PayItem item : allPayItem){
            	if(item.getStatus() == 2){
                	List<PayMoney> tempPayMoney = payMoneyDAO.getPayMoneyByItemID(account,item.getItemID());
                    oldDynamicPayMoney.addAll(tempPayMoney);
                }else{
                	List<PayMoney> tempPayMoney = payMoneyDAO.getPayMoneyByItemID(account,item.getItemID());
                    oldFixedPayMoney.addAll(tempPayMoney);
                }
            }
       
            List<PayMoney> fixedPayMoney = expenditureForm.getFixedPayMoney();
            List<PayMoney> dynamicPayMoney = expenditureForm.getDynamicPayMoney();
            List<PayMoney> deletedPayMoney = expenditureForm.getDeletedPayMoney();
            //固定成本
            Map<Integer,Integer> tempFixedCostRecord = new HashMap<Integer,Integer>();
            for(int i = 0;i < oldFixedPayMoney.size();i++){
            	tempFixedCostRecord.put(oldFixedPayMoney.get(i).getID(),i);
            }
            if(fixedPayMoney != null){
            	for (int i = 0; i < fixedPayMoney.size(); i++) {
            		PayMoney newObject = fixedPayMoney.get(i);
                    if(oldFixedPayMoney.size() == 0){
                    	int recordID = payMoneyDAO.getPayRecord(account,newObject.getID());
                        payMoney.setID(newObject.getID());
                        payMoney.setRecord(recordID);
                    	/*year = newObject.getDate_string().substring(3,7);
                    	mon = newObject.getDate_string().substring(0,2);*/
                        payMoney.setDate_string(newObject.getDate_string()+"-01");
                        payMoney.setMoney(newObject.getMoney());
                        payMoneyDAO.addPayMoney(payMoney);
                    }else{
                    	if(fixedPayMoney.size() == oldFixedPayMoney.size()){//沒有缺資料，直接修改
                    		PayMoney oldObject = oldFixedPayMoney.get(i); ;
                    		/*year = oldObject.getDate_string().substring(4,8);
                         	mon = oldObject.getDate_string().substring(0,2);
                         	oldObject.setDate_string(year+"-"+mon+"-01");*/
                            if (!oldObject.getDate().equals(newObject.getDate_string())
                            		|| oldObject.getMoney() != newObject.getMoney()) {
                            	newObject.setAccount(account);
                            	/*year = newObject.getDate_string().substring(4,8);
                            	mon = newObject.getDate_string().substring(0,2);*/
                                newObject.setDate_string(newObject.getDate_string()+"-01");
                                payMoneyDAO.modiPayMoney(newObject);
                            }
                    	}else{//有缺
                    		if(tempFixedCostRecord.containsKey(newObject.getID())){
                    			PayMoney oldObject = oldFixedPayMoney.get(tempFixedCostRecord.get(newObject.getID()));
                    			/*year = oldObject.getDate_string().substring(4,8);
                             	mon = oldObject.getDate_string().substring(0,2);
                             	oldObject.setDate_string(year+"-"+mon+"-01");*/
                    			if (!oldObject.getDate().equals(newObject.getDate_string())
                                		|| oldObject.getMoney() != newObject.getMoney()) {
                                	newObject.setAccount(account);
                                	/*year = newObject.getDate_string().substring(4,8);
                                	mon = newObject.getDate_string().substring(0,2);*/
                                	newObject.setDate_string(newObject.getDate_string()+"-01");
                                    payMoneyDAO.modiPayMoney(newObject);
                                }
                    		}else{
                        		int recordID = payMoneyDAO.getPayRecord(account,newObject.getID());
                                payMoney.setID(newObject.getID());
                                payMoney.setRecord(recordID);
                                /*year = newObject.getDate_string().substring(4,8);
                            	mon = newObject.getDate_string().substring(0,2);*/
                            	payMoney.setDate_string(newObject.getDate_string()+"-01");
                                //payMoney.setDate_string(newObject.getDate_string() + "-01");
                                payMoney.setMoney(newObject.getMoney());
                                payMoneyDAO.addPayMoney(payMoney);
                    		}
                    	}
                    }
            	}
            }
            //刪除
            if (deletedPayMoney != null) {
            	for (PayMoney p : deletedPayMoney) {
            		p.setAccount(account);
                    payMoneyDAO.delPayMoney(p);
            	}
            }
            //動態新增
            if(dynamicPayMoney != null){
            	for (int i = 0; i < dynamicPayMoney.size(); i++) {
            		PayMoney newObject = dynamicPayMoney.get(i);
            		if (newObject.getID() == 0){//被刪除
            			continue;
            		}else{
                    	if(oldDynamicPayMoney.size() > 0 && oldDynamicPayMoney.size() > i){//有舊資料
	                        PayMoney oldObject = oldDynamicPayMoney.get(i);
                			/*year = oldObject.getDate_string().substring(4,8);
                         	mon = oldObject.getDate_string().substring(0,2);
                         	oldObject.setDate_string(year+"-"+mon+"-01");*/
	                        if (!oldObject.getDate().equals(newObject.getDate_string() + "-01")
	                        		|| oldObject.getMoney() != newObject.getMoney()) {//有修改
	                        	newObject.setAccount(account);
                            	newObject.setDate_string(newObject.getDate_string()+"-01");
                                payMoneyDAO.modiPayMoney(newObject);
	                        }
                    	}else{//沒有舊資料
                    		payMoney.setID(newObject.getID());
                            int recordID = payMoneyDAO.getPayRecord(account,newObject.getID());
                            payMoney.setRecord(recordID);
                            /*year = newObject.getDate_string().substring(4,8);
                        	mon = newObject.getDate_string().substring(0,2);*/
                        	payMoney.setDate_string(newObject.getDate_string()+"-01");
                            //payMoney.setDate_string(newObject.getDate_string() + "-01");
                            payMoney.setMoney(newObject.getMoney());
                            payMoneyDAO.addPayMoney(payMoney);
                        }
            		}
            	}
            }
                       
            if(subClassID == 211){
            	model.setViewName("redirect:/placeComplete");
            }else if(subClassID == 212){
            	model.setViewName("redirect:/personPlace");
            }else if(subClassID == 213){
            	model.setViewName("redirect:/equipmentBuild");
            }else if(subClassID == 214){
            	model.setViewName("redirect:/trialProduction");
            }else if(subClassID == 215){
            	model.setViewName("redirect:/procureMaterial");
            }else if(subClassID == 216){
            	model.setViewName("redirect:/trialOperation");
            }else if(subClassID == 217){
            	model.setViewName("redirect:/developmentMarket");
            }else if(subClassID == 218){
            	model.setViewName("redirect:/marketing");
            }else if(subClassID == 219){
            	model.setViewName("redirect:/ventureCapitalPlanMap");
            }
        }else{
        	model.setViewName("redirect:/");
        }
        return model;
	}
	
	@RequestMapping(value = "/modiPersonPlaceStaffNum", method = RequestMethod.GET) // to modify the number of the staff
	public @ResponseBody String modiPersonPlaceStaffNum(int modiStaffNum, int hr_itemID, int hr_periodID,
			HttpSession session) { // should return HumanResourceContent, int or just void
		VentureChecklistDAO ventureChecklistDAO = (VentureChecklistDAO)context.getBean("ventureChecklistDAO");
		Member loginMember = (Member) session.getAttribute("loginMember");
		//HumanResourceContent humanResourceContent = ventureChecklistDAO.getHumanResourceContentByMember(loginMember);
		
		String hr_item = "";
		String hr_period = "";
		
		switch (hr_itemID) {
			case 0:
				hr_item = "M";
				break;
			case 1:
				hr_item = "R";
				break;
			case 2:
				hr_item = "S";
				break;
			case 3:
				hr_item = "O";
				break;
		}
		
		switch (hr_periodID) {
			case 0:
				hr_period = "0";
				break;
			case 1:
				hr_period = "1";
				break;
			case 2:
				hr_period = "2";
				break;
			case 3:
				hr_period = "3";
				break;
			case 4:
				hr_period = "4";
				break;
			case 5:
				hr_period = "5";
				break;
			case 6:
				hr_period = "6";
				break;
			case 7:
				hr_period = "7";
				break;
			case 8:
				hr_period = "8";
				break;
			case 9:
				hr_period = "9";
				break;
			case 10:
				hr_period = "10";
				break;
			case 11:
				hr_period = "11";
				break;
			case 12:
				hr_period = "12";
				break;			
		}
		int staffNum = ventureChecklistDAO.getSingleMemberResourceContentColumn(loginMember, hr_item, hr_period);
		ventureChecklistDAO.modiSingleMemberResourceContentColumn(loginMember, modiStaffNum, hr_item, hr_period);
		Payroll payroll = new Payroll();
		
		if(modiStaffNum > staffNum) {
			for(int n = staffNum + 1; n <= modiStaffNum; n ++) {
				payroll.setAccount(loginMember.getAccount());
				payroll.setItemID(hr_itemID);
				payroll.setPeriodID(hr_periodID);
				payroll.setEmployeeRecord(n);
				ventureChecklistDAO.insertSingleHumanResourceContentRecord(loginMember, payroll);
			}
		}else {
			for(int n = staffNum; n > modiStaffNum; n --) {
				payroll.setAccount(loginMember.getAccount());
				payroll.setItemID(hr_itemID);
				payroll.setPeriodID(hr_periodID);
				payroll.setEmployeeRecord(n);
				ventureChecklistDAO.deleteSingleHumanResourceContentRecord(loginMember, payroll);
			}
		}
		return Integer.toString(modiStaffNum);
	}
	
	@RequestMapping(value = "/savePersonPlace", method = RequestMethod.POST) // to save each tab
	public ModelAndView savePersonPlace(HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();
		VentureChecklistDAO ventureChecklistDAO = (VentureChecklistDAO)context.getBean("ventureChecklistDAO");
		
		return model;
	}
	@RequestMapping(value = "/addPersonPlace", method = RequestMethod.POST)
    public ModelAndView addPersonPlace(HttpServletRequest request, HttpSession session, String hr_periodID){
    	ModelAndView model = new ModelAndView();    	
    	Member loginMember = (Member) session.getAttribute("loginMember");
        if(loginMember != null){
        	PayrollDAO payrollDAO = (PayrollDAO) context.getBean("payrollDAO");
        	Payroll payroll = new Payroll();
        	String[] itemIDList = request.getParameterValues("itemID");
        	String[] payrollList = request.getParameterValues("payroll");
    		String[] trainingFeeList = request.getParameterValues("trainingFee");
    		
    		//紀錄該item人數到第幾筆
    		int item[] = new int[4];
    		item[0] = 0;
    		item[1] = 0;
    		item[2] = 0;
    		item[3] = 0;
    		if(itemIDList!=null || payrollList!=null || trainingFeeList!=null){
	    		for (int i = 0; i < payrollList.length; i++) {
	    			item[Integer.parseInt(itemIDList[i])]++;
	    			int record = item[Integer.parseInt(itemIDList[i])];
	    			payroll.setAccount(loginMember.getAccount());
	    			payroll.setItemID(Integer.parseInt(itemIDList[i]));
	    			payroll.setPeriodID(Integer.parseInt(hr_periodID));
	    			payroll.setPayroll(Integer.parseInt(payrollList[i]));
	    			payroll.setTrainingFee(Integer.parseInt(trainingFeeList[i]));  
	    			payroll.setEmployeeRecord(record);
	    			payrollDAO.update(payroll);
	    		}
	    		if(hr_periodID==null)
	    			hr_periodID ="";	    			
	            model.setViewName("redirect:/personPlace?t="+Integer.valueOf(hr_periodID));
    		}else
		    	model.setViewName("redirect:/");
        }
		return model;
	}
}
